#include <algorithm>
#include <list>

#include "subject.h"
#include "observer.h"

#include "uieventdispatcher.h"

void UIEventDispatcher::Attach( Observer* observer )
{
	if( observer == NULL )
	{
		return;
	}

	ObserverListIter it = std::find( _receiverList.begin(), _receiverList.end(), observer );

	if( it == _receiverList.end() )
	{
		_receiverList.push_back( observer );
	}
}

void UIEventDispatcher::Detach( Observer* observer )
{
	if( observer == NULL || _receiverList.empty() )
	{
		return;
	}

	ObserverListIter it = std::find( _receiverList.begin(), _receiverList.end(), observer );

	if( it != _receiverList.end() )
	{
		_receiverList.erase( it );
	}
}

void UIEventDispatcher::DetachAll()
{
	if( !_receiverList.empty() )
	{
		_receiverList.erase( _receiverList.begin(), _receiverList.end() );
	}
}

void UIEventDispatcher::Notify( void* info )
{
	ObserverListIter it = _receiverList.begin();

	while( it != _receiverList.end() )
	{
		( *it++ )->Update( info );
	}
}